package DatabasePackage;

import java.util.HashMap;
import java.util.Map;


/**
 * This class contains common functionality that some of our databases will need
 * 
 */
public class Utility {
  /**
   * Checks if the given parameter is null and prints accordingly.
   */
  public static void checkIfNull(Object param) {
    if (param == null) {
      StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
      int callerMethodLocation = 2;
      StackTraceElement callingContext = stackTraceElements[callerMethodLocation];
      String methodName = callingContext.getMethodName();
      String exception = "Method " + methodName + " has recieved a null parameter" + ", please send a correct parameter";
      throw new IllegalArgumentException(exception);
    }
  }
  
  /**
   * This method returns a partial record
   * 
   * @param columnType
   *          the column type from which we extract the string
   * @param columnId
   * @return A mapping that represents
   */
  public static <T> Map<String, String> createPartialRecord(Enum<?> columnType, T columnId) {
    String value = columnId.toString();
    // Creating the category to add
    Map<String, String> recordToAdd = new HashMap<String, String>();
    recordToAdd.put(columnType.toString(), value);
    return recordToAdd;
  }
  
  public static String castToString(Integer num){
    String res = num.toString();
    if(res.length()>10){
      return res.substring(0,9);
    }
    return res;
  }
  
  public static String castToString(Float num){
    String res = num.toString();
    if(res.length()>10){
      return res.substring(0,9);
    }
    return res;
  }
}
